# Filename: jit-mda-extract-snapshot.sh
# Usage: jit-mda-extract-snapshot <image name> <size>
# Example 1: jit-mda-extract-snapshot.sh vm01 4G
# Example 2: jit-mda-extract-snapshot.sh vm01 4096
#
# Creates a LVM and extracts the received file on it.
#
# Requires the 'pv' package.
# sudo apt-get install pv

imagename=$1
size=$2
LOCALFOLDER=/tmp
VOLUME_GROUP=vg_datastore
IMGPATH=$LOCALFOLDER/$imagename

echo `date +%H:%M:%S` Starting Script $0 $*
if [ -z "$imagename" -o -z "$size" ]; then
	echo Incorrect parameters
	echo "Usage: $0 <image name> <size>"
else
	lvcreate -L $size $VOLUME_GROUP -n ${imagename}-disk
	echo Copying $size bytes from $imagename disk at `date +%H:%M:%S` hours. Please wait.
	dd if=$IMGPATH | pv -s $(du -sb $IMGPATH | awk '{print $1}') | dd of=/dev/$VOLUME_GROUP/${imagename}-disk
	rm $LOCALFOLDER/$imagename
fi
echo `date +%H:%M:%S` Finishing Script $0
exit 0
